package com.wuda.servlet.user;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


@WebServlet("/user/exit")
public class Exit extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws  IOException {
		System.out.println("-----注销登录-----");
		HttpSession session = req.getSession(false); // 获取当前 session，如果没有则返回 null
		if (session != null) {
			session.invalidate(); // 注销 session，清除所有属性
		}
		resp.sendRedirect(req.getContextPath() + "/index.jsp"); // 重定向到首页
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp); // 将 POST 请求处理重定向到 doGet 方法
	}
}
